Skip to content

fix(api): explicit scalar cwe stays primary when cwes list is also supplied#15305

Merged
Maffooch merged 1 commit into
DefectDojo:devfrom
valentijnscholten:fix/api-v2-cwes-support
Jul 23, 2026
Merged

fix(api): explicit scalar cwe stays primary when cwes list is also supplied#15305
Maffooch merged 1 commit into
DefectDojo:devfrom
valentijnscholten:fix/api-v2-cwes-support

Conversation

@valentijnscholten

Copy link
Copy Markdown
Member

Summary

Fixes a precedence bug in the (unreleased, dev-only) multiple-CWE write support that #15143 added to the finding API, and hardens its test coverage.

  • The bug: when a request supplied both the scalar cwe and a cwes list, cwes[0] unconditionally overwrote the explicit scalar — the more specific input lost, on both create and update.
  • The fix: precedence now mirrors the established vulnerability_idscve pattern — an explicit scalar cwe in the request stays the primary and every cwes entry is persisted as an extra Finding_CWE row (save_cwes dedupes any overlap); when no scalar is supplied, cwes[0] becomes the primary and is mirrored into the scalar. Detection uses initial_data presence, the pattern this serializer already uses elsewhere.
  • Backward compatibility: wire shape is untouched (cwes remains the [{"cwe": "CWE-n"}] object list; no fields renamed, no response changes). The behavior change affects only the not-yet-released cwes field and only requests carrying both inputs at once, so no released consumer can depend on the old behavior.
  • Tests: +8 API cases in unittests/test_finding_cwe.py — precedence on create and update (both proven failing before the fix), cwes-only primary promotion, list replacement, omission leaves rows untouched, explicit-empty clears extras (with and without a scalar), and a read-back wire-shape guard. spectacular --fail-on-warn passes.
  • One documented quirk left as-is (deliberately, for a separate discussion): a scalar-only cwe change with cwes omitted does not resync the existing Finding_CWE rows, which can leave a stale primary row. Not enshrined in a test, so it stays cheap to change if desired.

…pplied

PR DefectDojo#15143 added multiple-CWE support (cwes object list, writable on
create and update). When a request supplied BOTH the scalar cwe and a
cwes list, cwes[0] unconditionally overwrote the explicit scalar - the
more specific input lost.

Precedence now mirrors the vulnerability_ids->cve pattern: an explicit
scalar cwe in the request stays the primary and every cwes entry is
treated as an extra row (save_cwes dedupes the overlap); without a
scalar, cwes[0] becomes the primary and is mirrored into the scalar.
Detection uses initial_data presence - the established pattern in this
serializer (FindingTemplateSerializer).

Behavior change only for the unreleased cwes field when both inputs
appear in one request; no released consumer can depend on the old
behavior. Wire shape unchanged.

+8 API tests: precedence on create/update, cwes-only primary promotion,
replace, omission-untouched, explicit-empty semantics, read-back shape.
@valentijnscholten valentijnscholten added this to the 3.1.300 milestone Jul 21, 2026
@valentijnscholten
valentijnscholten marked this pull request as ready for review July 21, 2026 18:36
@Maffooch
Maffooch merged commit d604c30 into DefectDojo:dev Jul 23, 2026
147 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants